using UnityEditor;
using System.IO;
using System.Linq;
using UnityEngine;

public class BuildScript
{
    public static void PerformBuild()
    {
        // 动态获取项目根路径
        string projectRoot = Path.GetDirectoryName(Application.dataPath);
        string outputPath = Path.Combine(projectRoot, "Build/Linux");

        // 强制清空并创建目录
        if (Directory.Exists(outputPath)) Directory.Delete(outputPath, true);
        Directory.CreateDirectory(outputPath);

        // 验证平台支持
        if (!BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Standalone, BuildTarget.StandaloneLinux64))
        {
            Debug.LogError("当前Unity未安装Linux平台支持模块！");
            EditorApplication.Exit(1);
            return;
        }

        // 构建Linux平台
        BuildPipeline.BuildPlayer(
            EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray(),
            Path.Combine(outputPath, "Game.x86_64"), // Linux可执行文件后缀
            BuildTarget.StandaloneLinux64,
            BuildOptions.ShowBuiltPlayer
        );

        // 验证输出
        if (!File.Exists(Path.Combine(outputPath, "Game.x86_64")))
        {
            Debug.LogError("构建失败：未生成可执行文件");
            EditorApplication.Exit(1);
        }
        else
        {
            Debug.Log($"构建成功！输出文件：{outputPath}/Game.x86_64");
        }
    }
}